﻿using GoodAdmin.System.UnifyResult;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Newtonsoft.Json;
using System.Drawing;

namespace GoodAdmin.Web.Core;

public class Startup : AppStartup
{
    public void ConfigureServices(IServiceCollection services)
    {
        //配置
        services.AddComponent<OptionComponents>();
        //授权
        services.AddComponent<AuthComponents>();


        services.AddControllers()
            .AddAppLocalization(settings =>
            {
                services.AddJsonLocalization(options => options.ResourcesPath = settings.ResourcesPath);
            })
            .AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;//忽略循环引用
                options.SerializerSettings.DateFormatString = "yyyy-MM-dd HH:mm:ss";// 时间格式化
            })
            .AddFriendlyException()
            .AddInjectWithUnifyResult<GoodUnifyResultProvider>();// 注册规范化结果状态码;


        Colorful.Console.WriteAsciiAlternating("GoodAdmin", new Colorful.FrequencyBasedColorAlternator(3, Color.Yellow, Color.GreenYellow));
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        // 添加规范化结果状态码，需要在这里注册
        app.UseUnifyResultStatusCodes();
        // 配置多语言
        app.UseAppLocalization();
        app.UseStaticFiles();

        app.UseHttpsRedirection();

        app.UseRouting();

        app.UseCorsAccessor();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseInject(string.Empty);

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
        });
    }
}
